﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using DataAccess;
using DevExpress.XtraEditors;
using QuanLyKhachSan.Validation;

namespace QuanLyKhachSan.FormThemCapNhat
{
    public partial class FormLoaiPhongCapNhat : XtraForm
    {
        public LoaiPhong loaiphong = new LoaiPhong();
        LoaiPhongManager lpManager = new LoaiPhongManager();
        public bool isInserted = false;
        public FormLoaiPhongCapNhat()
        {
            InitializeComponent();
        }

        public FormLoaiPhongCapNhat(LoaiPhong lpCapNhat)
        {
            InitializeComponent();
            KhoiTaoLoaiPhong(lpCapNhat);
        }

        private void KhoiTaoLoaiPhong(LoaiPhong lp)
        {
            txtMaLoaiPhong.Text = lp.MaLoaiPhong;
            txtTenLoaiPhong.Text = lp.TenLoaiPhong;
            cbbTrangThai.Text = lp.TrangThai;
            mtbGiaPhong.Text = lp.GiaPhong.ToString();
        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            ThaoTac();
        }

        private void ThaoTac()
        {
            loaiphong.MaLoaiPhong = this.txtMaLoaiPhong.Text;
            loaiphong.TenLoaiPhong = this.txtTenLoaiPhong.Text;
            loaiphong.GiaPhong = int.Parse(this.mtbGiaPhong.Text);
            loaiphong.TrangThai = this.cbbTrangThai.Text;
            ValidateResult vResult = lpManager.Validate(loaiphong);
            if (!vResult.Message.Any())
            {
                ActionResult aResult;
                if (System.String.Compare(btnThaoTac.Text, "Thêm", System.StringComparison.OrdinalIgnoreCase) == 0)
                {
                    aResult = lpManager.Them(loaiphong);
                }
                else
                {
                    aResult = lpManager.CapNhat(loaiphong);
                }
                if (!aResult.Exceptions.Any())
                {
                    isInserted = true;
                    Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    XtraMessageBox.Show(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                XtraMessageBox.Show(s);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormLoaiPhongCapNhat_Load(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            if (System.String.Compare(btnThaoTac.Text, "Thêm", System.StringComparison.OrdinalIgnoreCase) == 0)
            {
                txtMaLoaiPhong.Text = "";
                txtTenLoaiPhong.Text = "";
                mtbGiaPhong.Text = "000000";
                cbbTrangThai.SelectedIndex = 0;
            }
        }
    }
}
